package com.example.springbootdemo.configuration;

import com.example.springbootdemo.constant.RocketMQConstant;
import org.apache.rocketmq.client.apis.ClientConfiguration;
import org.apache.rocketmq.client.apis.ClientConfigurationBuilder;
import org.apache.rocketmq.client.apis.ClientException;
import org.apache.rocketmq.client.apis.ClientServiceProvider;
import org.apache.rocketmq.client.apis.producer.Producer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

/**
 * @ClassName: RocketMQConf
 * @Description:
 * @Author: TXW
 * @Date: 2025/7/18
 */
@Configuration
public class RocketMQConf {

    @Bean
    public ClientServiceProvider provider() {
        return ClientServiceProvider.loadService();
    }

    @Bean
    public ClientConfiguration configuration() {
        ClientConfigurationBuilder builder = ClientConfiguration.newBuilder().setEndpoints(RocketMQConstant.ADDRESS);
         return builder.setRequestTimeout(Duration.ofSeconds(30)).build();
    }

    @Bean
    public Producer producer(ClientConfiguration configuration) throws ClientException {
        ClientServiceProvider provider = ClientServiceProvider.loadService();

        return provider.newProducerBuilder()
                .setTopics(RocketMQConstant.TOPIC,RocketMQConstant.TOPIC2)
                .setClientConfiguration(configuration)
                .build();
    }


}
